// Class include
#include "CCommand.hpp"

// System includes

// Other includes
#include "CTrace.hpp"

CCommand::CCommand(const eCommandSource source, const unsigned char* rawBuffer)
: mCommandType(COMMAND_INVALID)
, mCommandSource(source)
, mValueInt32(0)
, mValueInt8_1(0)
, mValueInt8_2(0)
, mValueInt8_3(0)
, mValueInt8_4(0)
{
   if (0 != rawBuffer)
   {
      mCommandType = toCommand(rawBuffer[0]);

      // Warning! Assuming that (COMMAND_LENGTH-1) not more then sizeof(int)
      if (COMMAND_LENGTH - 1 != sizeof(int))
      {
         TRC_ERROR("(COMMAND_LENGTH - 1) != sizeof(int). mValueInt32 could be incorrect. Rebuild application with correct COMMAND_LENGTH or change type of mValue in CCommand.hpp");
      }

      for (int i=1; i<COMMAND_LENGTH; i++)
      {
         if (i!=1)   // No needs for first element
         {
            mValueInt32 = mValueInt32 << 8;   // Bits in byte :)
         }
         mValueInt32 += rawBuffer[i];
      }
      
      mValueInt8_1 = rawBuffer[1];
      mValueInt8_2 = rawBuffer[2];
      mValueInt8_3 = rawBuffer[3];
      mValueInt8_4 = rawBuffer[4];
   }
}

CCommand::CCommand(const eCommandSource source, const eCommand mType, const unsigned char mValue)
: mCommandType(mType)
, mCommandSource(source)
, mValueInt32(0)
, mValueInt8_1(mValue)
, mValueInt8_2(0)
, mValueInt8_3(0)
, mValueInt8_4(0)
{

}

void CCommand::getRawBuffer(unsigned char* pBuffer)
{
   if (0 == pBuffer) return;

   pBuffer[0] = mCommandType;
   pBuffer[1] = mValueInt8_1;
   pBuffer[2] = mValueInt8_2;
   pBuffer[3] = mValueInt8_3;
   pBuffer[4] = mValueInt8_4;
}

int CCommand::getValue32() const
{
   return mValueInt32;
}

eCommand CCommand::getType() const
{
   return mCommandType;
}

int CCommand::getValue8_1() const
{
   return mValueInt8_1;
}

int CCommand::getValue8_2() const
{
   return mValueInt8_2;
}

int CCommand::getValue8_3() const
{
   return mValueInt8_3;
}

int CCommand::getValue8_4() const
{
   return mValueInt8_4;
}

eCommandSource CCommand::getSource() const
{
   return mCommandSource;
}